package reflect.cons;

import org.junit.Test;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

public class ConstructorDemo1 {
    //获取到无参构造，并创建对象，给对象赋值
    @Test
    public void testConstructor1() throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        //第一步：获取到class对象
        Class userClass = User.class;


        //第二步：基于class对象，获取到构造器（constructor）
        Constructor constructor = userClass.getConstructor();

        //第三步：利用构造器，创建对象
        User user= (User) constructor.newInstance();

        //第四步：给对象赋值
        user.setName("nihao");
        user.setPassword("123");
        System.out.println(user);
    }
}
